package com.earn.main;

import com.earn.utils.CoverFlow;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class HomeActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		setTheme(R.style.CustomTheme);
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		CoverFlow cf = new CoverFlow(this);
		cf.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg));
		cf.setAdapter(new ImageAdapter(this));
		ImageAdapter imageAdapter = new ImageAdapter(this);
		cf.setAdapter(imageAdapter);
		cf.setSelection(3, true);
		cf.setAnimationDuration(1000);
		setContentView(cf);
		cf.setOnItemClickListener(new ItemListener());
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		
		tvTitle = (TextView) findViewById(R.id.Titletext);
		tvTitle.setText("Earn");

		btnBack = (Button) findViewById(R.id.TitleBackBtn);

		btnBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
						KeyEvent.KEYCODE_BACK);
				onKeyDown(KeyEvent.KEYCODE_BACK, newEvent);
			}
		});

	}
	
	public TextView tvTitle;
	public Button btnBack;
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 
        if (keyCode == KeyEvent.KEYCODE_BACK
                 && event.getRepeatCount() == 0) {
        	askForOut();
         }
         return super.onKeyDown(keyCode, event);
     }
	
	private void askForOut() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setTitle("确定退出").setMessage("确定退出？").setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton("取消",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).setCancelable(false).show();
	}
	
	class ItemListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
			Intent intent = new Intent();
			switch(position){
			case 0:
				intent.setClass(HomeActivity.this, MigrateCustomerization.class);
				break;
			case 1:
//				intent.setClass(HomeActivity.this, NewImageWiki.class);
				break;
			case 2:
//				intent.putExtra("from", "home");
//				intent.setClass(HomeActivity.this, History.class);
				intent.setClass(HomeActivity.this, MigrateListActivity.class);
				break;
			case 3:
				intent.setClass(HomeActivity.this, HousingResourceListActivity.class);
				break;
			case 4:
//				intent.setClass(HomeActivity.this, HousingResourceListActivity.class);
				break;
			case 5:
//				intent.setClass(HomeActivity.this, About.class);
				break;
			}
			startActivity(intent);
//			overridePendingTransition(R.anim.fade, R.anim.hold);
		}
    }

}
